package rabbitmq.subscribe;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import rabbitmq.common.Const;

/**
 * @author zss
 * @date 2021/8/27 17:02
 * @Email:451682479@qq.com
 */
public class SubProvider {

    public final static String QUEUE_NAME = "sub_queue1";
    private final static String VIRTUAL_HOST = "devHost";

    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Const.host);
        factory.setUsername(Const.username);
        factory.setPassword(Const.password);
        factory.setVirtualHost(VIRTUAL_HOST);

        String virtualHost = factory.getVirtualHost();

        System.out.println("virtualHost = " + virtualHost);

        try {
            Connection connection = factory.newConnection();

            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

            for (int i = 0; i < 10000; i++) {
                String message = i + "logs-" + UUID.randomUUID().toString() + new Date().toString();

                channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                Thread.sleep(500);
            }

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
